home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacOberonLite 1.0.1 / Docu / MakeMacApp.Txt next >
Encoding:
Text File  |  1994-04-30  |  11.6 KB  |  162 lines  |  [TEXT/ObLi]

  1. This document was first written for MacOberon. Therefore some actions may 
  2. only refer to MacOberon and not MacOberonLite.
  3.  
  4. ________________________________________________________________________
  5.  
  6. Writing Macintosh Applications using MacOberon
  7. Oliver Dreer
  8.  
  9. This document explains how to create standalone Macintosh applications using MacOberon. As an example, SampleApp.Mod, a small text editor is included, to show how simple it is to write a Macintosh application. Besides that, the Oberon compiler is a very fast and efficient 32-bit compiler, that produces very dense code, and because of this, programs written by MacOberon are running faster and are smaller than similar programs developed using other programming environments.
  10.  
  11.  
  12. Introduction
  13.  
  14. The Oberon language and the MacOberon compiler are very fast and efficient. So why not use it to develop standard Macintosh applications? In fact, MacOberon can be used to develop such applications without any major limitation. The biggest problem is to implement all Macintosh routines, Types and Constants. In the initial release, we ported only the most important routines and types, and only a few constants. We also followed a new approach, by grouping the data as the new Inside Mac Series does. However, by the time this project finished, not all new Inside Mac books were available, so some data may be at the wrong place, missing or even obsolete.
  15.     When porting old Pascal programs to MacOberon, the most expensive changes are to fix the toolbox routine names by adding the right Toolbox module name, for example LineTo becomes MacImaging.LineTo. Most data types and constants can be used without changes, however there are some limitations. See below for more details about this.
  16.  
  17.  
  18. The Macintosh Toolbox
  19.  
  20. Using the Macintosh Toolbox files within MacOberon is a bit different from other programming environments like MPW or Think Pascal/C. Most of all, the data is structured like the new Inside Mac books (from now on IM). There are quite a lot of toolbox calls that changed their spelling in the new IMs, for example the routine that extracts a menu text was called GetItem and spells now GetMenuItemText. In most cases, we use the old spelling, in some special cases both spellings are implemented. (For example DisposPtr and DisposePtr )
  21.     However even more important are the changes for register-based toolbox traps. Most of the memory manager routines and some of the file manager routines are register-based. Therefore, you won't be able to use these routines in a common way. You have to push your variables into the registers and read the results afterwards from the registers. When using the IM just use the assembly-language information that always follows the description.
  22. Example : 
  23.  
  24.     MPW Pascal :                                MacOberon :
  25.     myPtr:=NewPtr(theSize);               SYSTEM.PUTREG(0,theSize);
  26.                                                       ME.NewPtr;
  27.                                                       SYSTEM.GETREG(8,myPtr);
  28.  
  29.  
  30. At first sight, the Oberon version looks very clumsy, but when looking at the produced assembler code, it is obvious, that using MacOberon saves you many instructions when heavily using register-based traps! 
  31. Assembler code produced:
  32.  
  33.     CLR.L      -(SP)                                MOVE.L    theSize,D0
  34.     MOVE.L    theSize,-(SP)                   _NewPtr
  35.     JSR        xy                                      MOVE.L    A0,@myPtr
  36.     MOVE.L    (SP)+,@myPtr
  37.  
  38.     xy:
  39.     MOVEA.L    (SP)+,A1
  40.     MOVE.L    (SP)+,D0
  41.     _NewPtr
  42.     MOVE.L    A0,(SP)
  43.     JMP        (A1)
  44.  
  45. Another major difference is how these external Macintosh datatypes and routines can be used. When using MPW Pascal, you only have to import the toolbox files by writing USES <x ,y ,z > and you could use all types and routines like described in the IM. In MacOberon, you also have to import the toolbox files, however you have to precede all types and routines by their module name. It is very convenient to use the alias import feature of Oberon the abbreviate the names of the toolbox modules.
  46.  
  47. Here are all toolbox files, and their standard abbreviation:
  48.  
  49. ME    : MacMemory            ( Memory  )
  50. IM    : MacImaging            ( QuickDraw, Printing, Color )
  51. TB    : MacToolbox            ( Menus, Windows, Dialogs, Controls )
  52. MF    : MacFiles                ( Filesystem )
  53. MP    : MacProcesses            ( Processes, Tasks, Interrupts )
  54. MO    : MacOSUtils            ( Gestalt, OS Utilites )
  55. MT    : MoreMacToolbox            ( Resources, Help, Sound )
  56. TE    : MacText                ( TEdit, Textdrawing, Scripts )
  57. MC    : MacIAC                ( Apple Events, Communications )
  58.  
  59. However these toolbox files are not complete, for example, only a very small amount of the constants have been declared. Therefore it will be necessary to add your own data to these files, or even better, create a new toolbox file containing your additions, so you won't need to update all files when new versions of MacOberon are released.
  60.     You absolutly need the corresponding Inside Macintosh volumes when adding new routines, types or constanst. It's also helpful if you can get hold of some Interface files of other development platforms. These files are containing all datatypes and constants, and most routines.
  61.     When implementing a new routine, just copy the hex code from the interface file. Don't forget the minus right after the PROCEDURE keyword, this tells the compiler to use the inline hex code instead of a procedure body..
  62. Example :
  63.  
  64.     Trap only : 
  65.         PROCEDURE- PaintRect*(r:Rect) 
  66.             0A8H,0A2H;
  67.     Trap with routine selector : 
  68.         PROCEDURE- CTabChanged*(port:GrafPtr) 
  69.             020H,03CH,000H,004H,000H,007H,0ABH,01DH;
  70.  
  71. There are also some routines described in IM, that are not implemented as traps or traps with routine selectors. These are declared as [NOT IN ROM]. There is currently no official way to use these routines. Either you implement the functionality by yourself, or you disassemble the Interface library from another programming environment, like Interface.o from MPW.
  72.  
  73. While implementing the toolbox files, simplifications were made to some datatypes. For example most of the types that are aliases to other simple types were left out.
  74. These are for example:
  75.  
  76.     OSErr = INTEGER    (also QDError)
  77.     Fixed = LONGINT
  78.     Size = LONGINT
  79.     SignedByte = SHORTINT
  80.  
  81. In some special cases, we also use LONGINT instead of ME.Ptr to ease the use of the address.
  82.     The OSType in IM is declared as ARRAY [0..3] OF CHAR, however, this is nothing else than a LONGINT, so when using OSTypes you can pass a LONGINT, for example 'TEXT' = 54455854H. Two characters stand for the hex representation of the ASCII value of that character.
  83.     Another important difference is the byte-length of a character. Macintosh toolbox routines always use two bytes for a CHAR instead of one when using MacOberon. Therefore all toolbox routines using CHARs declare their CHARs as INTEGERs. Just pass ORD(myChar) to such routines where myChar is of Oberontype CHAR. Also remember that the function CHR() also returns a singlebyte CHAR type.
  84.  
  85. If you want to implement Macintosh datatypes using Ptrs or Handles always follow this scheme :
  86.  
  87.     myDesc* = RECORD ... END;
  88.     myPtr* = POINTER- TO myDesc;
  89.     myHandle* = POINTER- TO RECORD p*:myPtr END;
  90.  
  91. The minus after POINTER hides this pointer from garbage collection. Therefore you also have to allocate and deallocate these pointers using NewPtr/DisposePtr, and won't be able to use the Oberon NEW procedure or the garbage collector for these pointers. When dereferencing the handle, just use myHandle.p.<myEntry>.
  92.  
  93.  
  94. AppLinker
  95.  
  96. Using the Applinker is quite simple. There are, however, some rules you have to observe. Because you are using the MacOberon Compiler, the applications produced also need at least an 68020 processor. And if you are using REAL or LONGREAL within your application, FPU code is produced, so you have to check for the presence of an FPU by yourself. The check for an 68020 processor will be made by the bootloader, each time when starting the created application.
  97.     If you want to use Oberon memory allocation and garbage collection, you also have to be sure, that the application gets at least a 1MB memory partition for proper operation.
  98.  
  99. Usage of the AppLinker:
  100.     AppLinker.Link SampleApp [Creator]
  101.  
  102. To link an application, you have to provide the module name of your main module. If you specify a creator, the bundle bit will also be set.
  103.  
  104. To launch an application within MacOberon, you can use : 
  105.     AppLinker.Launch SampleApp
  106.  
  107. If there exists a file named SampleApp.res, then all resources from this file are copied to your application. For proper operation you have to include at least a SIZE resource id=-1 with the following flags set to one:
  108.     accept suspend/resume, activate on FG switch and can background
  109. If this resource is not provided, some malfunction may occur when switching between your and other applications.
  110.     The AppLinker also adds two code resources (id 0 and 1) containing the bootloader and two ALRT/DITL combinations (id 32766 / 32767) for the error messages and the trap handler. The object code is stored within the data fork of the application.
  111.  
  112.  
  113. Oberon Features
  114.  
  115. The SYSTEM module contains some very useful procedures, some of them are even required to develop Macintosh applications. Most of these procedures are translated into single instructions assembler code. Check the documentation of Oberon for more information about all SYSTEM procedures.
  116. The most important procedures for Macintosh applications are:
  117.  
  118.     SYSTEM.PUTREG(n,x) : Register n := x
  119.     SYSTEM.GETREG(n,v) : v := Register n
  120.  
  121. IF v is of a real type and 0<=n<=7, then floating point registers are used. Otherwise, 0<=n<=7 stands for dataregisters D0-D7, 8<=n<=15 for address registers A0-A7.
  122.  
  123.     SYSTEM.PUT(a,x)    : Mem[a] := x
  124.     SYSTEM.GET(a,v)    : v := Mem[a]
  125.     SYSTEM.MOVE(s,d,n) : Mem[d] … Mem[d+n-1] := Mem[s] … Mem[s+n-1]
  126.     SYSTEM.VAL(T,x)    : x interpreted as of type T
  127.     SYSTEM.ADR(v)      : address of variable v (LONGINT)
  128.  
  129. It is possible to use Garbage Collection and Run-Time type tests within your Macintosh applications. However, you have to import the module Runtime, and call the GC by yourself.
  130.     Ed.Open Runtime.Mod
  131.  
  132.  
  133. Examples
  134.  
  135. There are two examples containing Macintosh toolbox routines. The first one is a small standalone text editor using the build-in Macintosh TextEdit, that can be used as a framework for other applications.
  136.     Ed.Open SampleApp.Mod
  137.  
  138. The second one is an example of using the Oberon System and the Macintosh Interface together. It has only one command, Import, that uses a Macintosh file selector box to choose a plain text file and then opens a simple edit viewer, that contains the text. For example, you can use this command to import text files from other directories into MacOberon.
  139.     Ed.Open MacEd.Mod  ***Does not apply to MacOberonLite**
  140.  
  141.  
  142. Tips & Hints
  143.  
  144. What's following now is a list of some usful hints, not so obvious to a novice user:
  145.  
  146. - Use the brower '+' option if you are not sure how your datatypes are allocated. This is very useful if you are implementing new Toolbox datatypes. You will be given the offsets of the record entries and therefore you can calculate the size of these entries. See the Brower.Tool for more information about the Browser.  ***Does not apply to MacOberonLite***
  147.  
  148. - For string conversion between Oberon strings (null terminated) and Macintosh Pascal strings, you can use SetStr255 and GetStr255 from the MacToolbox module.
  149.  
  150. - When using CASE constructs, use the ELSE path for unwanted results. Otherwise traps may occur.
  151.  
  152. - When getting system error id=28 increase the memory partition of your program.
  153.  
  154. - If you are using REAL or LONGREAL operations, please don't forget to check for the presence of an FPU processor, otherwise your program just crashes on machines without an FPU (or an FPU emulator).
  155.  
  156.  
  157. Further Reading
  158.  
  159. - Inside Macintosh Volume I, Chapter 4 (Assembly Language)
  160. - Inside Macintosh Toolbox Essentials (for SampleApp )
  161. - Oberon Guide (for Oberon System) 
  162. - Oberon Report (for Oberon programming)